package com.ruoyi.controller.app;

import com.ruoyi.app.domain.params.SendCodeParam;
import com.ruoyi.app.domain.vo.UserGetInfoVO;
import com.ruoyi.app.domain.vo.UserLoginVO;
import com.ruoyi.app.service.IUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.app.domain.constants.CustomPromptConstant;
import com.ruoyi.app.domain.params.UserLoginParam;
import com.ruoyi.app.domain.params.UserRegisterParam;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.utils.ApiThreadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "用户信息")
@RequestMapping("/api/user")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RestController
public class UserController extends BaseController {

    private final IUserService userService;

    @ApiOperation("获取当前用户信息")
    @GetMapping("/getInfo")
    public ApiResult<UserGetInfoVO> getInfo() {
        return ApiResult.success(userService.getInfoById(ApiThreadUtils.getUser().getId()));
    }

    @ApiOperation("发送验证码")
    @PostMapping("/sendCode")
    public ApiResult<Void> sendCode(@RequestBody SendCodeParam sendCodeParam) {
        userService.sendCode(sendCodeParam);
        return ApiResult.success(CustomPromptConstant.SEND_SUCCESS);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public ApiResult<Void> userRegister(@RequestBody UserRegisterParam userRegisterParam) {
        userService.register(userRegisterParam);
        return ApiResult.success(CustomPromptConstant.REGISTER_SUCCESS);
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public ApiResult<UserLoginVO> userLogin(@RequestBody UserLoginParam userLoginParam) {
        return ApiResult.success(userService.login(userLoginParam));
    }
}
